<template>
	<view class="container">
		<view class="queryForm">
			<uni-forms ref="queryRef" :model="queryParams" label-width="70px" label-align="right">
				<uni-forms-item label="部门名称">
					<uni-easyinput v-model="queryParams.deptName" placeholder="请输入部门名称" />
				</uni-forms-item>
				<uni-forms-item label="状态" name="status">
					<xy-select v-model="queryParams.status" :data="sys_normal_disable" clearable />
				</uni-forms-item>
				<uni-forms-item label="父部门">
					<xy-drawer-tree-select :data="deptTree" v-model="parentId" value-name="deptId" label-name="deptName"
						@change="handleQuery()" />
				</uni-forms-item>
				<uni-forms-item label="操作">
					<button class="mini-btn xy-btn" type="primary" size="mini" @click="handleQuery()">搜索</button>
					<button class="mini-btn xy-btn" type="primary" size="mini" @click="resetQuery()">重置</button>
					<button class="mini-btn xy-btn" type="default" size="mini" @click="handleAdd()"
						v-if="proxy.$auth.hasPermi('system:dept:add')">添加</button>
				</uni-forms-item>
			</uni-forms>
		</view>
		<!-- @iconClick="iconClick" -->
		<view v-for="(item,index) in deptList" :key="index">
			<uni-card>
				<view class="uni-body">
					<view>
						<text>部门名称：</text>
						<text>{{ item.deptName }}</text>
					</view>
					<view>
						<text>排序：</text>
						<text>{{ item.orderNum }}</text>
					</view>
					<view>
						<text>状态：</text>
						<dict-tag :options="sys_normal_disable" :value="item.status" />
					</view>
					<view>
						<text>创建时间：</text>
						<text>{{ item.createTime }}</text>
					</view>
				</view>
				<view slot="actions" class="card-actions">
					<button class="mini-btn xy-btn" type="primary" size="mini" @click="handleUpdate(item)"
						v-if="proxy.$auth.hasPermi('system:dept:edit')">编辑</button>
					<button class="mini-btn xy-btn" type="warn" size="mini" @click="handleDelete(item)"
						v-if="proxy.$auth.hasPermi('system:dept:remove')">删除</button>
				</view>
			</uni-card>
		</view>
	</view>
</template>

<script setup>
	import {
		listDept,
		getDept,
		delDept,
		addDept,
		updateDept,
		listDeptExcludeChild
	} from "@/pages_work/api/system/dept";
	import {
		ref,
		getCurrentInstance
	} from 'vue'
	import {
		onLoad,
		onPullDownRefresh,
		onReachBottom,
	} from '@dcloudio/uni-app'
	const {
		proxy
	} = getCurrentInstance();
	const {
		sys_normal_disable
	} = proxy.useDict("sys_normal_disable");

	/** 查询部门列表所需的参数 */
	const queryParams = ref({
		pageNum: 1,
		pageSize: 10,
	})

	/** 筛选选中的父部门ID **/
	const parentId = ref(0);

	/** 部门列表 */
	const deptList = ref([]);

	/** 部门树*/
	const deptTree = ref([]);

	/** 查询部门列表 */
	const getList = () => {
		listDept(queryParams.value).then(res => {
			deptList.value = res.data.filter(item => item.parentId == parentId.value)
			deptTree.value = proxy.handleTree(res.data, 'deptId')
		})
	}

	/** 搜索按钮操作 */
	function handleQuery() {
		queryParams.value.pageNum = 1;
		getList();
	};

	/** 重置按钮操作 */
	function resetQuery() {
		// dateRange.value = [];
		proxy.resetForm("queryRef");
		queryParams.value = {
			pageNum: 1,
			pageSize: 10,
		};
		parentId.value = 0;
		handleQuery();
	};

	/** 添加部门信息 */
	const handleAdd = () => {
		uni.$once('update', function(){
			handleQuery()
		})
		proxy.$tab.navigateTo("form?title=添加部门&deptId=");
	}

	/** 编辑部门信息 */
	const handleUpdate = (row) => {
		uni.$once('update', function(){
			handleQuery()
		})
		proxy.$tab.navigateTo("form?title=编辑部门&deptId=" + row.deptId);
	}

	/** 删除按钮操作 */
	const handleDelete = (row) => {
		const ids = row.deptId;
		proxy.$modal.confirm('是否确认删除部门编号为"' + ids + '"的数据项？').then(function() {
			return delDept(ids);
		}).then(() => {
			handleQuery();
			proxy.$modal.msgSuccess("删除成功");
		}).catch(() => {});
	};

	/** 生命周期函数--下拉刷新 */
	onPullDownRefresh(() => {
		handleQuery()
		setTimeout(function() {
			uni.stopPullDownRefresh();
		}, 1000)
	})

	handleQuery();
</script>

<style lang="scss">
	@import "@/static/scss/work/index.scss";
</style>